package Top_Interview_Questions.Array;

/**
 * @Author: 吕庆龙
 * @Date: 2020/3/12 12:45
 * <p>
 * 功能描述:
 */
public class _0048 {

    public static void main(String[] args) {
        _0048 test = new _0048();
//        int[][] matrix = {
//                {1,2,3},
//                {4,5,6},
//                {7,8,9},
//
//        };
        int[][] matrix1 = {
                {1,2,3,4},
                {5,6,7,8},
                {9,10,11,12},
                {13,14,15,16}
        };
        test.rotate2(matrix1);
    }

    public void rotate2(int[][] matrix) {
        int n = matrix.length;
        // 上下翻转，两种写法
        // 第一种
        //  for (int i =0; i < n /2 ; i++ ){
        //      for (int j =0; j < n; j ++){
        //          int tmp = matrix[i][j];
        //          matrix[i][j] = matrix[n-i-1][j];
        //          matrix[n-i-1][j] = tmp;
        //      }
        //  }
        // 第二种
        for (int i = 0; i < n / 2; i ++){
            int[] tmp = matrix[i];
            matrix[i] = matrix[n - i - 1];
            matrix[n - i - 1] = tmp;
        }
        //System.out.println(Arrays.deepToString(matrix));
        // 对角翻转
        for (int i = 0; i < n; i ++){
            for (int j= i + 1; j < n; j++){
                int tmp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = tmp;
            }
        }
    }



    /**
     * 找规律
     * https://leetcode-cn.com/problems/rotate-image/solution/yi-ci-xing-jiao-huan-by-powcai/
     */
    public void rotate1(int[][] matrix) {
        int n = matrix.length;
        for (int i = 0; i < n/2; i++ ) {
            for (int j = i; j < n - i - 1; j ++ ){
                int tmp = matrix[i][j];
                matrix[i][j] = matrix[n-j-1][i];
                matrix[n-j-1][i] = matrix[n-i-1][n-j-1];
                matrix[n-i-1][n-j-1] = matrix[j][n-i-1];
                matrix[j][n-i-1] = tmp;
            }
        }
    }



    /**
     * 转置+翻转
     */
    public void rotate(int[][] matrix) {
        int n = matrix.length;

        // transpose matrix
        for (int i = 0; i < n; i++) {
            for (int j = i; j < n; j++) {
                int tmp = matrix[j][i];
                matrix[j][i] = matrix[i][j];
                matrix[i][j] = tmp;
            }
        }
        System.out.println();
        // reverse each row
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n / 2; j++) {
                int tmp = matrix[i][j];
                matrix[i][j] = matrix[i][n - j - 1];
                matrix[i][n - j - 1] = tmp;
            }
        }
    }


}
